<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要BB的内容（做多吐槽120字）" maxlength="120">

        </textarea>
        <mt-button type="primary" size="large">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments" :key="item.add_time">
                <div class="cmt-title">
                    第{{ i + 1 }}楼 &nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}
                </div>
                <div class="cmt-body">
                    {{item.content === '' ? '此用户很懒，啥都没说就走了' : item.content }}
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
import { Toast } from 'mint-ui'
export default {
    data(){
        return{
            pageindex: 1, //默认展示第一页数据
            comments: [] //所有评论数据
        }
    },
    created(){
        this.getComments();
    },
    methods:{
        getComments(){
            this.$http.get('/api/comments' + this.id  + this.pageindex).then((resp)=>{
                resp = resp.body 
                if(resp.errno == 0){
                    // this.comments = resp.data
                    //获取新评论的时候不要吧之前的覆盖了，而是应该以老数据拼接新数据
                    this.comments = this.comments.concat( resp.data)
                }
            })
        },
        getMore(){
            this.pageindex++;
            if(this.pageindex > 2){
                Toast("没有啦！！！")
            }else{
                this.getComments();
            }
           
        }
    },
    props: ["id"]
}
</script>
<style lang="scss" scoped>
.cmt-container{
    h3{
        font-size: 16px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
         font-size: 13px;
         
         
         .cmt-item{
             background-color:rgb(248, 248, 248);
             margin: 5px 0;
             border-bottom: 1px solid rgb(192, 191, 191);
            .cmt-title{
                line-height: 30px;
                font-weight: bold;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
         }
        
    }
}
</style>